package Thread;

class Singleton {
    private static Singleton instance = new Singleton();

    public  static Singleton getInstance() {
        return instance;
    }
    private Singleton() {

    }
}

class SingletonLazy {
    private static volatile  SingletonLazy instance = null;

    private static SingletonLazy getInstance() {
        Object locker = new Object();
        //第一个if判断是否创建了instance
        if(instance == null) {
            synchronized (locker) {
                if(instance == null) {
                    //在多线程的情况下 判定
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
    private SingletonLazy() {

    }

}
public class Demo9 {
    public static void main(String[] args) {
        System.out.println(Singleton.getInstance());
    }
}
